package top.songxs.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import top.songxs.base.BaseController;
import top.songxs.entity.Admin;
import top.songxs.service.AdminService;
import top.songxs.service.RoleService;
import top.songxs.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;
    @RequestMapping
    public String findPage(HttpServletRequest request, Model model){
        Map<String, Object> filters = super.getFilters(request);
        PageInfo<Admin> page = this.adminService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "admin/index";
    }
    @GetMapping("/create")
    public String create(){
        return "admin/create";
    }
    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("/save")
    public String save(Admin admin){
        admin.setHeadUrl("http://qny.songxs.top/2c5cbef6-3464-4674-8bf0-ccde22aafe51.jpg");
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        this.adminService.insert(admin);
        return "common/successPage";
    }
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        Admin admin = this.adminService.getById(id);
        model.addAttribute("admin",admin);
        return "admin/edit";
    }

    @PostMapping("/update")
    public String update(Admin admin){
        this.adminService.update(admin);
        return "common/successPage";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        this.adminService.delete(id);
        return "redirect:/admin";
    }
    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable Long id,Model model){
        model.addAttribute("id",id);
        return "admin/upload";
    }
    @RequestMapping("/upload/{id}")
    public String upload(@PathVariable Long id, MultipartFile file) throws IOException {
        //上传图片到七牛云  lvy.ou2.jpg
        String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = UUID.randomUUID().toString();//没有扩展名  32141234aerqreqadfadf2342134
        fileName+=extName; //32141234aerqreqadfadf2342134.jpg
        QiniuUtil.upload2Qiniu(file.getBytes(),fileName);
        //写数据到数据库中
        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl("http://qny.songxs.top/"+fileName);
        adminService.update(admin);
        return "common/successPage";
    }
    @Reference
    private RoleService roleService;
    /**
     * 进入分配角色页面
     * @param adminId
     * @return
     */
    @GetMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable  Long adminId,Model model){
        Map<String, Object> roleMap = this.roleService.findRoleByAdminId(adminId);
        //model.addAttribute("roleMap",roleMap); //roleMap.noAssignRoleList
        // roleMap.put("noAssignRoleList", noAssignRoleList);
        // roleMap.put("assignRoleList", assignRoleList);
        //["noAssignRoleList": noAssignRoleList,"assignRoleList", assignRoleList]
        model.addAllAttributes(roleMap);
        model.addAttribute("adminId",adminId);
        return "admin/assignShow";

    }
    /*
    页面："1,4,6,"
     List<Long> roleIds: ["1","4","6",null]
     */
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId, Long [] roleIds){
        this.roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }

    /**
     * 获取当前登录信息
     * @return
     */
    @GetMapping("/getInfo")
    @ResponseBody
    public Object getInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();//登录用户的信息，有什么呢

        return authentication.getPrincipal();
    }

}
